﻿@{
    ViewBag.Title = "PostJsonSubmit";
    Layout = "~/Views/Shared/_Page.cshtml";
}

<h2>PostJsonSubmit</h2>
<br />
<input id="btnSubmit" type="button" value="submit" />

<script type="text/javascript">
    $(document).ready(function () {
        var url = "/Ajax/GetJsonResult";

        //var jsonData = initObjString();
        var jsonData = [ { "Name": "Kelvin", "Age": "35" }, { "Name": "Hdy", "Age": "25" }, { "Name": "Cxm", "Age": "24" } ];
        ZEUS.DEBUG.log("Request Data: ");
        ZEUS.DEBUG.log(JSON.stringify(jsonData));
        //ZEUS.DEBUG.log(ZEUS.JSON.JSONstringify(jsonData));
        ZEUS.DEBUG.log("**************************************************************");
        $('#btnSubmit').click(function () {
            $.ajax({
                url: url,
                type: "POST",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(jsonData),
                success: function (data) {
                    ZEUS.DEBUG.log("Response Data: ");
                    ZEUS.DEBUG.log(JSON.stringify(data));
                    ZEUS.DEBUG.log("1. Name: " + data[0].Name);
                    ZEUS.DEBUG.log("2. Age: " + data[0].Age);
                    //ZEUS.DEBUG.log(ZEUS.JSON.JSONstringify(data));
                }
            });
        });
    });

    /*
    function initJsonData() {
        var jsonData = "";

        jsonData += "[";
        for (var i = 0; i < 10; i++) {
            jsonData += "{ 'Name': 'person" + i
                + "', 'Age': '" + (20 + i);
            if (i < 9) {
                jsonData += "'}, ";
            } else {
                jsonData += "'} ";
            }
        }
        jsonData += "]";

        return jsonData;
    }
    */
    
    function initObjString() {
        var jsonData = "";

        for (var i = 0; i < 10; i++) {
            if (i != 0) {
                jsonData += ', ';
            }
            
            jsonData += '"persons[' + i + '].Name":"' + 'person' + i;
            jsonData += '", ';
            jsonData += '"persons[' + i + '].Age":"' + i + '"';
        }

        jsonData = "{ " + jsonData + " }";

        return jsonData;
    }
</script>